//
//  NoNetworkView.m
//  lxd
//
//  Created by Aaron on 2017/5/23.
//  Copyright © 2017年 陈晨昕. All rights reserved.
//

#import "NoNetworkView.h"

@interface NoNetworkView()

@property(nonatomic,strong) NoNetworkView * view;

@end

@implementation NoNetworkView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    [self setupUI];

    }
    return self;
}

//singleton
+ (NoNetworkView *)sharedNoNetworkView{
    static NoNetworkView* instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

- (void)setupUI{
    
    self.frame = SCREENBOUNDS;
    
    self.backgroundColor  = BACKGROUND_COLOR;
    UIImageView * imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"no-internet"]];
    imgV.contentMode = UIViewContentModeScaleToFill;
    imgV.bounds = CGRectMake(0, 0, 200, 200);
    [self addSubview:imgV];
    imgV.center = self.center;
    imgV.centerY = self.centerY - 100;
    
    //prompt
    UIButton *prompt = [[UIButton alloc]init];
    [prompt setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [prompt setTitle:NSLocalizedString(@"点击以重新加载", nil) forState:UIControlStateNormal];
    [prompt sizeToFit];
    [self addSubview:prompt];
    
    prompt.centerX = self.centerX;
    prompt.centerY = imgV.bottom + 40;
    
    [prompt addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];
    
}

+ (NoNetworkView*)addNoInternetViewToSuperView:(UIView *)superView{

    [superView addSubview:[NoNetworkView sharedNoNetworkView]];
    [superView bringSubviewToFront:[NoNetworkView sharedNoNetworkView]];
    return [NoNetworkView sharedNoNetworkView];
}

+ (void)remove{
    [[self sharedNoNetworkView] removeFromSuperview];
}

- (void)click{
    if (self.refreshBlock) {
        self.refreshBlock();
    }
}


@end
